package com.yanqu.road.server.gameplayer.module.activity.horserun.event;

import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;
import com.yanqu.road.entity.activity.horserun.config.HorseRunEventConfig;
import com.yanqu.road.entity.activity.horserun.config.HorseRunTextConfig;
import com.yanqu.road.entity.enums.activity.horserun.eHorseRunEventState;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.timer.banquet.CrossBanquetSyncMemberTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class HorseRunBaseEvent {

    public static Logger logger = LogManager.getLogger(HorseRunBaseEvent.class.getName());

    public HorseRunProto.EventResult.Builder meet(HorseRunUserGamingData userGamingData, HorseRunConfig config, HorseRunEventConfig eventConfigById){
        return null;
    }

    public HorseRunProto.EventResult.Builder deal(HorseRunUserGamingData userGamingData,HorseRunDealEventArg arg, HorseRunConfig config, HorseRunEventConfig eventConfigById){
        return null;
    }

    /**
     *  type 1为遇到 2为处理
     */
    public void addUserLog(HorseRunUserGamingData userGamingData, HorseRunConfig config,int type,int eventId,List<Integer> param, HorseRunProto.EventResult.Builder builder){

        Map<Integer, HorseRunTextConfig> textConfigs = config.getTextConfigs();
        if (!textConfigs.containsKey(eventId)){
            return;
        }
        HorseRunTextConfig textConfig = textConfigs.get(eventId);
        if (textConfig == null){
            return;
        }
        //1为仅遇到 2为仅处理 3都有 4为冒险子事件特殊 5为开始游戏特殊
        int textAddType = textConfig.getType();
        if (textAddType != 3){
            if (type != textAddType){
                return;
            }
        }

        HorseRunProto.HorseRunEventLog.Builder logBuild = HorseRunProto.HorseRunEventLog.newBuilder();

        int day = userGamingData.getDay();
        logBuild.setDay(day);

        Map<Integer, List<List<Integer>>> eventMap = userGamingData.getEventMap();
        Map<Integer, List<List<Integer>>> tempMap = new ConcurrentHashMap<>();

        //深拷贝一个
        // 深拷贝 eventMap 到 tempMap
        for (Map.Entry<Integer, List<List<Integer>>> entry : eventMap.entrySet()) {
            int key = entry.getKey();
            List<List<Integer>> value = entry.getValue();

            // 创建新的列表进行深拷贝
            List<List<Integer>> newList = new ArrayList<>();
            for (List<Integer> sublist : value) {
                // 深拷贝每个子列表
                newList.add(new ArrayList<>(sublist));
            }

            // 将深拷贝的 key-value 对放入新的 map
            tempMap.put(key, newList);
        }

        List<List<Integer>> dayList = tempMap.computeIfAbsent(day, k -> new ArrayList<>());
        List<Integer> eventList = new ArrayList<>();
        //第一个数据是事件id
        eventList.add(eventId);
        logBuild.setEventId(eventId);

        //第二个是遭遇或处理
        eventList.add(type);
        logBuild.setType(type);

        //最后看看加不加param
        if (param!=null){
            eventList.addAll(param);
            logBuild.addAllParam(param);
        }

        dayList.add(eventList);

        builder.addEventLog(logBuild);

        //超过400条 移除最旧的
        if (tempMap.size() > 400){
            int min = Collections.min(tempMap.keySet());
            tempMap.remove(min);
        }

        userGamingData.setEventMap(tempMap);

        userGamingData.setUpdateOption();
        return;
    }

    public void eventDone(HorseRunUserGamingData userGamingData){
        setEventState(userGamingData,eHorseRunEventState.done);
    }

    public void eventGoing(HorseRunUserGamingData userGamingData){
        setEventState(userGamingData,eHorseRunEventState.onGoing);
    }

    public void eventNoDeal(HorseRunUserGamingData userGamingData){
        setEventState(userGamingData,eHorseRunEventState.noDeal);
    }



    public void setEventState(HorseRunUserGamingData userGamingData,eHorseRunEventState state){
        userGamingData.setEventState(state.getValue());
    }


}
